package com.hxzy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hxzy.annoation.Log;
import com.hxzy.mapper.EmpMapper;
import com.hxzy.pojo.Emp;
import com.hxzy.pojo.PageBean;
import com.hxzy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author wzm
 * @create 2024-03-04 15:33
 * project_name:JavaWeb2401
 * @Description
 */
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1.查询出总记录数
       Long count = empMapper.count();

        //2.查询符合分页的结果列表
        Integer begin = (page-1) * pageSize;//(当前页- 1)*size
       List<Emp> empList = empMapper.getPage(begin,pageSize);//limit start,size

        return new PageBean(count,empList);
    }

    @Override
    public PageBean pageByHelper(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //通过PageHelper设置分页的数据
        PageHelper.startPage(page,pageSize);
        //以上代码下方的查询会自动分页
        List<Emp> empList = empMapper.getAll(name,gender,begin,end);
        //获取到分页的结果
        Page<Emp> p = (Page<Emp>)empList;
        log.info("empList:{}",empList);
        log.info("p:{}",p);
        //通过结果获取分页数据
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Log
    @Override
    public void deleteByIds(List<Integer> ids) {
       empMapper.deleteById(ids);
    }

    @Log
    @Override
    public void add(Emp emp) {
        //补全时间信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp findById(Integer id) {
        return empMapper.selectEmpById(id);
    }


    @Log
    @Override
    public void update(Emp emp) {
        //更新时间
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUserNameAndPassWord(emp);
    }


}
